package com.itheima.app.manager.omsManager;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.commons.PageBean;
import com.itheima.commons.R;
import com.itheima.domain.db.oms.OmsOrder;
import com.itheima.domain.db.oms.OmsOrderItem;
import com.itheima.domain.db.oms.OmsOrderOperateHistory;
import com.itheima.dto.OmsOrderDto;
import com.itheima.service.omsService.OrderItemService;
import com.itheima.service.omsService.OrderOperateHistoryService;
import com.itheima.service.omsService.OrderService;
import com.itheima.vo.OmsOrderVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Component
public class OmsOrderManager {
    @DubboReference
    private OrderService orderService;

    /**
     * 查询订单
     *
     * @param omsOrderDto 参数
     * @param pageNum     页数
     * @param pageSize    每页条数
     * @return
     */
    public R findByParams(OmsOrderDto omsOrderDto, Integer pageNum, Integer pageSize) {
        PageBean page = orderService.findByParams(omsOrderDto, pageNum, pageSize);
        return R.ok(page);
    }

    @DubboReference
    OrderItemService orderItemService;

    @DubboReference
    OrderOperateHistoryService orderOperateHistoryService;

    /**
     * 获取订单详情：订单信息、商品信息、操作记录
     *
     * @param id
     * @return
     */
    public OmsOrderVo getInfoByOrderId(Integer id) {
        OmsOrderVo vo = new OmsOrderVo();
        //订单信息
        OmsOrder order = orderService.getById(id);
        BeanUtil.copyProperties(order, vo);
        //订单商品信息
        List<OmsOrderItem> orderItemList = orderItemService.findByOrderId(id);

        vo.setOrderItemList(orderItemList);
        //操作记录
        List<OmsOrderOperateHistory> historyList = orderOperateHistoryService.findByOrderId(id);

        vo.setHistoryList(historyList);
        return vo;
    }

    /**
     * 批量删除订单
     *
     * @param ids
     * @return
     */
    public void updateDelete(Integer[] ids) {
        orderService.updateDelete(ids);
    }

    /**
     * 批量关闭订单
     *
     * @param ids
     * @param note
     */
    public void updateClose(Integer[] ids, String note) {
        orderService.updateClose(ids, note);
    }

    /**
     * 批量发货
     *
     * @param maps
     */
    public void updateDelivery(LinkedHashMap[] maps) {
        orderService.updateDelivery(maps);
    }

    /**
     * 修改收货人信息
     * @param map
     */
    public void updateReceiverInfo(Map map) {
        orderService.updateReceiverInfo(map);
    }

    /**
     * 备注订单
     * @param id 订单id
     * @param note 备注信息
     * @param status 订单状态
     */
    public void updateNote(Integer id, String note, Integer status) {
        orderService.updateNote(id,note,status);
    }

    /**
     * 修改订单费用信息
     * @param map
     */
    public void updateMoneyInfo(Map map) {
        orderService.updateMoneyInfo(map);
    }
}
